package bilby;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;

public class BornAt {

    public static final String biblyIndex = "bilby";
    public static final String foxIndex = "fox";
    public static final String catIndex = "cat";
    public static Integer idConstructorBilby = 0;
    public static Integer idConstructorFox = 0;
    public static Integer idConstructorCat = 0;

    public static Integer idLen = 3;
    public static String pathStart = "src\\bilby\\populationStart.txt";
    public static String pathEnd = "src\\bilby\\populationFinal.txt";


    /**
     * 得到兔耳袋狸的id构造函数
     *
     * @return {@link String}
     */
    public static String getIdConstructorBilby() {
        String p = "B";
        idConstructorBilby += 1;

        Integer integer = idConstructorBilby;
        int count = 1;
        while (integer / 10 > 0) {
            count += 1;
            integer /= 10;
        }
        StringBuilder res = new StringBuilder();
        res.append("B");
        for (int i = 0; i < idLen - count; i++) {
            res.append("0");
        }
        res.append(idConstructorBilby.toString());
        return res.toString();
    }


    public static String getIdConstructorFox() {
        idConstructorFox += 1;

        Integer integer = idConstructorFox;
        int count = 1;
        while (integer / 10 > 0) {
            count += 1;
            integer /= 10;
        }
        StringBuilder res = new StringBuilder();
        res.append("F");
        for (int i = 0; i < idLen - count; i++) {
            res.append("0");
        }
        res.append(idConstructorFox.toString());
        return res.toString();
    }

    public static String getIdConstructorCat() {
        idConstructorCat += 1;

        Integer integer = idConstructorCat;
        int count = 1;
        while (integer / 10 > 0) {
            count += 1;
            integer /= 10;
        }
        StringBuilder res = new StringBuilder();
        res.append("C");
        for (int i = 0; i < idLen - count; i++) {
            res.append("0");
        }
        res.append(idConstructorCat.toString());
        return res.toString();
    }

    public static String[] readTxt() {
        String[] strArr = new String[10];
        int count = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(pathStart)));//构造一个BufferedReader类来读取文件
            String s = null;
            //使用readLine方法，一次读一行
            while ((s = br.readLine()) != null) {
                String res = System.lineSeparator() + s;
                if ("".equals(res)) {
                    continue;
                }
                strArr[count] = res.replace("\r\n", "");
                count += 1;
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strArr;
    }


    /**
     * String bilbyNum = split[0];
     * String foxNum = split[1];
     * String catNum = split[2];
     * 导出读取文件后的关键信息
     * @param strArr
     */
    public static List<String[]> extractKey(String[] strArr) {
//        String [] txt = readTxt();
        // 每个地址名对应的物种数量 对应下标拿对应的地点编号-1的元素
        List<String[]> listNum = new ArrayList<>();
        for (int i = 0; i < strArr.length; i++) {
            String[] split = strArr[i].split(",");
            listNum.add(split);
        }
        return listNum;
    }

    /**
     *
     * @param num 创建宝宝的数量
     * @param localtion
     * @param isNewBorn
     * @param index 传入一个标识，创建对应的宝宝
     * @return
     */
    public static RespUtil careateBabyObjetc(String num, String localtion, Boolean isNewBorn, String index) {
        int number = Integer.parseInt(num);

        RespUtil respUtil = new RespUtil();
        switch (index) {
            case "bilby":
                ArrayList<Bilby> list1 = new ArrayList<>();
                for (int i = 0; i < number; i++) {
                    Bilby bilby = new Bilby(getIdConstructorBilby(), true, isNewBorn, localtion, Newborn.BILBY_BORN);
                    list1.add(bilby);
                }
                respUtil.setListBibly(list1);
                break;
            case "fox":
                ArrayList<Fox> list2 = new ArrayList<>();
                for (int i = 0; i < number; i++) {
                    Fox fox = new Fox(getIdConstructorFox(), true, isNewBorn, 3, localtion, Newborn.FOX_BORN);
                    list2.add(fox);
                }
                respUtil.setListFox(list2);
                break;
            case "cat":
                ArrayList<Cat> list3 = new ArrayList<>();
                for (int i = 0; i < number; i++) {
                    Cat cat = new Cat(getIdConstructorCat(), true, localtion, isNewBorn, 3, Newborn.CAT_BORN);
                    list3.add(cat);
                }
                respUtil.setListCat(list3);
                break;
        }
        respUtil.setIndex(index);
        return respUtil;
    }


    /**
     * @param list 初始化10个地区的baby
     * @return {@link Location[]}
     */
    public static Location[] createBaby(List<String[]> list) {
        Location[] arrLocation = new Location[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Location location = new Location();
            String[] str = list.get(i);

            ArrayList<Bilby> bilbies;
            ArrayList<Fox> foxes;
            ArrayList<Cat> cats;

            bilbies = careateBabyObjetc(str[0], i + 1 + "", false, biblyIndex).getListBibly();
            foxes = careateBabyObjetc(str[1], i + 1 + "", false, foxIndex).getListFox();
            cats = careateBabyObjetc(str[2], i + 1 + "", false, catIndex).getListCat();


            location.setName(i + 1 + "");
            location.setBilbyArrayList(bilbies);
            location.setFoxArrayList(foxes);
            location.setCatArrayList(cats);

            arrLocation[i] = location;
        }
        return arrLocation;
    }


    /**
     * init创造宝宝
     * 从txt文档中读取数据，为10个地区分别创建了对应的物种
     *
     * @return {@link Location[]}
     */
    public static Location[] initCreateBaby() {
        // 调用文件读取方法,拿到信息
        String[] txt = readTxt();
        List<String[]> list = extractKey(txt);
        Location[] baby = createBaby(list);
        return baby;
    }

//    public static void main(String[] args) {
//        Location[] locations = initCreateBaby();
//        for (Location x : locations) {
//            System.out.println(x);
//        }
//        String idBilby = getIdConstructorBilby();
//        String idFox = getIdConstructorFox();
//        String idCat = getIdConstructorCat();
//        System.out.println("10个地点累计对应物种总数-{" + idBilby + ":" + idFox + ":" + idCat + "}");
//
//    }


    /**
     * 概率
     * 传入一个概率比如，15%，num = 15
     *
     * @param num 半分比的数字
     * @return boolean
     */
    public static boolean probability(int num) {
        Random random = new Random();
        // [0,99] -> [1,100]
        int nextInt = random.nextInt(100) + 1;
        if (nextInt <= num) {
            return true;
        }
        return false;
    }
}
